<template>
	<view class="sun-index">
		<view class="sun-logo-box"></view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 28rpx;height:39rpx;" src="@/static/imgs/mobile_icon.png" />
				<text class="label-text"></text>
			</view>
			<view class="sun-input-box">
				<input v-model="state.user.userEmail" type="text" placeholder="邮箱"
					placeholder-class="placeholder-class" />
				<image @click="state.user.userEmail=''" class="close-icon" src="@/static/imgs/close_icon.png" />
			</view>
		</view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 29rpx;height:29rpx;" src="@/static/imgs/code_icon.png" />
				<text class="label-text"></text>
			</view>
			<view class="sun-input-box">
				<input v-model="state.user.code" type="text" placeholder="验证码" placeholder-class="placeholder-class" />
				<text class="code-text" :class="{ gray: state.showTime }" @click="handleGetCodeClick">发送验证码</text>
			</view>
		</view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 29rpx;height:37rpx;" src="@/static/imgs/pwd_icon.png" />
				<text class="label-text"></text>
			</view>
			<view class="sun-input-box">
				<input v-model="state.user.userPassword" type="password" placeholder="密码"
					placeholder-class="placeholder-class" />
				<image @click="state.user.userPassword=''" class="close-icon" src="@/static/imgs/close_icon.png" />
			</view>
		</view>
		<view class="login-btn-box">
			<view class="login-btn" @click="handleSubmit">注册账号</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		onMounted
	} from 'vue'

	const state = reactive({
		user: {
			userEmail: '',
			userPassword: '',
			code: ''
		},
		countTime: 60,
		currentCountTime: 0,
		showTime: false,
		timeId: null
	})

	onMounted(() => {
		state.currentCountTime = state.countTime
	})

	function handleSubmit() {
		uni.request({
			url: 'http://175.178.249.171:8092/api/user/register',
			method: 'POST',
			data: JSON.stringify(state.user),
			success: (res) => {
				if (res.data.data === "注册成功") {
					uni.showToast({
						title: "注册成功",
						icon: 'none',
						duration: 1500
					})
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else {
					uni.showToast({
						title: "注册失败",
						icon: 'none',
						duration: 1500
					})
				}
			},
		})
	}

	function handleGetCodeClick() {
		if (state.user.userEmail !== '') {
			uni.request({
				url: 'http://175.178.249.171:8092/api/user/send/' + state.user.userEmail,
				method: 'POST',
				success: (res) => {
					uni.showToast({
						title: "邮件已发送",
						icon: 'none',
						duration: 1500
					})
				},
				fail: (res) => {}
			})
		}
	}
</script>

<style scoped>
	.sun-logo-box {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 750rpx;
		height: 300rpx;
	}

	.sun-icon-img {
		width: 120rpx;
		height: 120rpx;
	}

	.sun-logo {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 180rpx;
		height: 180rpx;
		border-radius: 15rpx;
		background-color: #12C8B9;
		box-shadow: 0rpx 0rpx 30rpx rgba(18, 200, 185, 0.5);
	}

	.close-icon {
		width: 36rpx;
		height: 34rpx;
	}

	.sun-login-box {
		padding: 20rpx 60rpx;
	}

	.sun-label {
		display: flex;
		align-items: center;
	}

	.label-text {
		margin-left: 16rpx;
		font-weight: 500;
		color: #272e2d;
		font-size: 30rpx;
	}

	.sun-input-box {
		display: flex;
		align-items: center;
		height: 100rpx;
		border-bottom: 1rpx solid #eee;
		padding: 0px 10rpx;
	}

	.sun-input-box input {
		flex: 1;
	}

	.placeholder-class {
		font-size: 30rpx;
		color: #C0C0C0;
	}

	.sun-tip {
		display: flex;
		justify-content: space-between;
		padding: 0rpx 68rpx;
	}

	.sun-tip-text {
		color: #30C6B3;
	}

	.login-btn-box {
		position: absolute;
		bottom: 129rpx;
		left: 0;
		width: 750rpx;
		padding: 50rpx 68rpx 0rpx;
	}

	.login-btn {
		height: 82rpx;
		border-radius: 41rpx;
		background-color: #12C8B9;
		box-shadow: -1px 12px 11px 0px rgba(16, 170, 157, 0.4);
		text-align: center;
		line-height: 82rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #fff;
	}

	.code-text {
		font-size: 28rpx;
		color: #30C6B3;
	}

	.gray {
		color: #C0C0C0;
	}
</style>
